﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CustomControls {
  /// <summary>
  /// Extended ErrorProvider
  /// </summary>
  public class ExtendedErrorProvider : ErrorProvider {

    private List<Control> controlList = new List<Control>( );

    /// <summary>
    /// Gets the controls containing an error.
    /// </summary>
    public Control[ ] ControlList {
      get { return controlList.ToArray( ); }
    }

    /// <summary>
    /// Gets the error count.
    /// </summary>
    public int ErrorCount {
      get {
        return controlList.Count;
      }
    }

    /// <summary>
    /// Gets a value indicating whether this instance has errors.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance has errors; otherwise, <c>false</c>.
    /// </value>
    public bool HasErrors {
      get {
        return controlList.Count > 0;
      }
    }

    /// <summary>
    /// Sets the errormessage for the specified control.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="message">The message.</param>
    public new void SetError( Control control, string message ) {
      base.SetError( control, message );
      if ( string.IsNullOrEmpty( message ) ) {
        if ( controlList.Contains( control ) ) {
          controlList.Remove( control );
        }
      } else {
        if ( !controlList.Contains( control ) ) {
          controlList.Add( control );
        }
      }
    }

  }
}
